﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;
using System.Collections.Generic;//Please add references
namespace EnterpriseFund.DAL
{
    /// <summary>
    /// 数据访问类:t_Key_NSRXX
    /// </summary>
    public partial class t_Key_NSRXX
    {
        public t_Key_NSRXX()
        { }
        #region  BasicMethod

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(EnterpriseFund.Model.t_Key_NSRXX model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into fund.t_Key_NSRXX(");
            strSql.Append("NSRDZDAH,FReportName)");
            strSql.Append(" values (");
            strSql.Append("@NSRDZDAH,@FReportName)");
            SqlParameter[] parameters = {
					new SqlParameter("@NSRDZDAH", SqlDbType.Decimal,13),
					new SqlParameter("@FReportName", SqlDbType.NVarChar,50)};
            parameters[0].Value = model.NSRDZDAH;
            parameters[1].Value = model.FReportName;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public bool AddExist(EnterpriseFund.Model.t_Key_NSRXX model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"IF NOT  EXISTS(SELECT TOP 1 * FROM  fund.t_Key_NSRXX where NSRDZDAH=@NSRDZDAH and FReportName=@FReportName )
                 BEGIN ");
            strSql.Append("insert into fund.t_Key_NSRXX(");
            strSql.Append("NSRDZDAH,FReportName)");
            strSql.Append(" values (");
            strSql.Append("@NSRDZDAH,@FReportName) End");
            SqlParameter[] parameters = {
					new SqlParameter("@NSRDZDAH", SqlDbType.Decimal,13),
					new SqlParameter("@FReportName", SqlDbType.NVarChar,50)};
            parameters[0].Value = model.NSRDZDAH;
            parameters[1].Value = model.FReportName;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public bool AddList(List<EnterpriseFund.Model.t_Key_NSRXX> modellist)
        {

            StringBuilder strSql = new StringBuilder();
            SqlConnection conn = new SqlConnection(DbHelperSQL.connectionString);
            conn.Open();
            SqlTransaction tran = conn.BeginTransaction();
            SqlCommand command = new SqlCommand();
            command.CommandTimeout = 60 * 60;
            try
            {

                foreach (EnterpriseFund.Model.t_Key_NSRXX model in modellist)
                {
                    strSql = new StringBuilder();
                    strSql.Append(@"IF NOT  EXISTS(SELECT TOP 1 * FROM  fund.t_Key_NSRXX where NSRDZDAH=@NSRDZDAH and FReportName=@FReportName )
	                 BEGIN ");
                    strSql.Append("insert into fund.t_Key_NSRXX(");
                    strSql.Append("NSRDZDAH,FReportName)");
                    strSql.Append(" values (");
                    strSql.Append("@NSRDZDAH,@FReportName) End");
                    SqlParameter[] parameters = {
						new SqlParameter("@NSRDZDAH", SqlDbType.Decimal,13),
						new SqlParameter("@FReportName", SqlDbType.NVarChar,50)};
                    parameters[0].Value = model.NSRDZDAH;
                    parameters[1].Value = model.FReportName;

                    command = new SqlCommand(strSql.ToString(), conn, tran);
                    command.Parameters.AddRange(parameters);
                    command.CommandTimeout = 60 * 60;
                    command.ExecuteNonQuery();
                }
                tran.Commit();
                return true;
            }
            catch (System.Exception ex)
            {
                tran.Rollback();
                return false;
            }
        }

        public bool delList(List<EnterpriseFund.Model.t_Key_NSRXX> modellist)
        {

            StringBuilder strSql = new StringBuilder();
            SqlConnection conn = new SqlConnection(DbHelperSQL.connectionString);
            conn.Open();
            SqlTransaction tran = conn.BeginTransaction();
            SqlCommand command = new SqlCommand();
            command.CommandTimeout = 60 * 60;
            try
            {

                foreach (EnterpriseFund.Model.t_Key_NSRXX model in modellist)
                {
                    strSql = new StringBuilder();
                    strSql.Append(@"DELETE fund.t_Key_NSRXX where NSRDZDAH=@NSRDZDAH and FReportName=@FReportName");
	                
                    SqlParameter[] parameters = {
						new SqlParameter("@NSRDZDAH", SqlDbType.Decimal,13),
						new SqlParameter("@FReportName", SqlDbType.NVarChar,50)};
                    parameters[0].Value = model.NSRDZDAH;
                    parameters[1].Value = model.FReportName;

                    command = new SqlCommand(strSql.ToString(), conn, tran);
                    command.Parameters.AddRange(parameters);
                    command.CommandTimeout = 60 * 60;
                    command.ExecuteNonQuery();
                }
                tran.Commit();
                return true;
            }
            catch (System.Exception ex)
            {
                tran.Rollback();
                return false;
            }
        }


        public bool btnAddAll(string FReportName)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"DELETE FROM fund.t_Key_NSRXX WHERE FReportName=@FReportName
INSERT INTO fund.t_Key_NSRXX( NSRDZDAH,FReportName)   (SELECT NSRDZDAH ,@FReportName FROM ltais.DJ_NSRXX )");
            SqlParameter[] parameters = {
										new SqlParameter("@FReportName", SqlDbType.NVarChar,50)};
            parameters[0].Value = FReportName;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public bool btnDeleteAll(string FReportName)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"DELETE FROM fund.t_Key_NSRXX WHERE FReportName=@FReportName");
            SqlParameter[] parameters = {
										new SqlParameter("@FReportName", SqlDbType.NVarChar,50)};
            parameters[0].Value = FReportName;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(decimal NSRDZDAH, string FReportName)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from fund.t_Key_NSRXX ");
            strSql.Append(" where NSRDZDAH=@NSRDZDAH and FReportName=@FReportName ");
            SqlParameter[] parameters = {
					new SqlParameter("@NSRDZDAH", SqlDbType.Decimal,13),
					new SqlParameter("@FReportName", SqlDbType.NVarChar,50)			};
            parameters[0].Value = NSRDZDAH;
            parameters[1].Value = FReportName;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string NSRDZDAHlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from fund.t_Key_NSRXX ");
            strSql.Append(" where NSRDZDAH in (" + NSRDZDAHlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        #endregion  BasicMethod
        #region  ExtensionMethod

        #endregion  ExtensionMethod
    }
}

